<?php
/**
 * Created by JetBrains PhpStorm.
 * User: AliRiza
 * Date: 22.12.2012
 * Time: 14:27
 * To change this template use File | Settings | File Templates.
 */
$url = $_POST['url'];
$url = str_replace("https","http",$url);
echo getMetaTag(str_replace("%26","&",$url));

function getMetaTag($url){
    $tags = array();
    try{
        $tags = get_meta_tags($url);
    }
    catch(Exception $e){
        echo $e;
    }

    $title="";
    $description="";
    if(array_key_exists("title",$tags)){
        $title = $tags['title'];
    }
    else{
        $page = file_get_contents($url);
        $titleStart=strpos($page,'<title>')+7;
        $titleLength=strpos($page,'</title>')-$titleStart;
        $title=substr($page,$titleStart,$titleLength);
        $title=trim($title);
    }
    if(array_key_exists("description",$tags)){
        $description = $tags['description'];
    }
    else{
        $description = "";
    }

    $response = "{";
    $response = $response."\"Title\":\"".$title."\"";
    $response = $response.",\"Description\":\"".$description."\"";
    $response = $response."}";
    return $response;
}